﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Light.Framework.Contract;
using Light.Framework.Model;
using System.Data;
using Light.Utility;

namespace Light.Framework.Linq
{
    public class AbstractBLL<T> where T : IEntity
    {
        protected IDataAccess<T> IDAL { get; set; }

        #region CRUD

        /// <summary>
        /// 新增【包含子项的数据项，不能调用此方法， 请使用Submit】
        /// </summary>
        /// <returns></returns>
        public virtual OperateResult Create(T obj)
        {
            return Excute(new Action<T>(IDAL.Insert), obj, OperateType.Create);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public virtual OperateResult Delete(T data)
        {
            return Excute(IDAL.Delete, data, OperateType.Delete);
        }

        /// <summary>
        /// 修改【包含子项的数据项，不能调用此方法， 请使用Submit】
        /// </summary>
        /// <returns></returns>
        public virtual OperateResult Modify(T obj)
        {
            return Excute(new Action<T>(IDAL.Update), obj, OperateType.Update);
        }


        public virtual OperateResult Add(List<T> list)
        {
            return Excute(IDAL.Insert, list, OperateType.Create);
        }

        /// <summary>
        /// 提交
        /// </summary>
        /// <returns></returns>
        public virtual OperateResult Submit(T obj)
        {
            if (obj.ID <= 0)
            {
                return Create(obj);
            }
            else
            {
                return Modify(obj);
            }
        }

        protected List<T> CacheResult = new List<T>();

        /// <summary>
        /// 查看所有
        /// </summary>
        /// <returns></returns>
        public List<T> GetAll()
        {
            return IDAL.SelectAll();
        }

        ///// <summary>
        ///// 查询
        ///// </summary>
        ///// <returns>List集合列表</returns>
        //public virtual List<T> SearchList()
        //{
        //    List<T> list = new List<T>();

        //    if (IDAL is ERP.MySqlAccess.ISearchableAccess<T>)
        //    {

        //        ERP.MySqlAccess.ISearchableAccess<T> iSearch = IDAL as ERP.MySqlAccess.ISearchableAccess<T>;

        //        try
        //        {
        //            CacheResult = iSearch.Select(searchItem, curTenant.ID, curUser.ID, CurType, CurState);
        //        }
        //        catch (Exception ex)
        //        {
        //            throw ex;
        //        }
        //    }
        //    return list;
        //}

        ///// <summary>
        ///// 向表格插入数据
        ///// </summary>
        ///// <param name="t"></param>
        ///// <param name="dt"></param>
        //protected abstract void InsertToTable(T t, DataTable dt);

        /// <summary>
        /// 查看详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual T GetDetails(int id)
        {
            return IDAL.SelectById(id);
        }

        /// <summary>
        /// 执行增，删，改操作
        /// </summary>
        /// <param name="method">方法名</param>
        /// <param name="data">操作对象</param>
        /// <param name="type">操作类型</param>
        /// <returns></returns>
        protected OperateResult Excute<TObj>(Action<TObj> method, TObj data, OperateType type)
        {
            OperateResult result = new OperateResult();
            try
            {
                method(data);
                result.IsSucced = true;
                string optStr = string.Empty;

                switch (type)
                {
                    case OperateType.Create: result.Message = "添加成功！"; break;
                    case OperateType.Update: result.Message = "更新成功！"; break;
                    case OperateType.Delete: result.Message = "删除成功！"; break;
                    default: break;
                }
            }
            catch (Exception ex)
            {
                switch (type)
                {
                    case OperateType.Create: result.Message = "添加失败"; break;
                    case OperateType.Update: result.Message = "更新失败"; break;
                    case OperateType.Delete: result.Message = "删除失败"; break;
                    default: break;
                }
                result.IsSucced = false;
                result.Message += "Sorry, system error occured :" + ex.Message;
            }


            return result;


        }

        #endregion
    }
}
